Repository navigation
Derive a provider's component key from its trait path, and document the key - #296
Merged
Merged
Conversation
`#[cgp_impl]`, `#[cgp_provider]`, and `#[cgp_new_provider]` named the component key after the provider trait's last segment, so a header written as `impl crate::components::Greeter` still needed `GreeterComponent` imported. The key now keeps the header's path and appends `Component` to its last segment, dropping the trait's generic arguments, so such a header resolves the key without an import. The key's last segment carries the trait identifier's span instead of the macro's call site. An unresolved key is then reported on the trait's name in the header rather than on the whole attribute, and with both names in scope Rust Analyzer's hover on the trait shows the key too. Go-to-definition on the trait still gives one target, since the marker struct is spanned on the same provider identifier as the trait. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A higher-order provider's inner bound gains an `IsProviderFor` counterpart naming the provider's key. With the header written by path, that counterpart names the key by the same path; a snapshot pins it and a context wires the provider over an inner one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The provider trait that `#[cgp_component]`, `#[cgp_type]`, and
`#[cgp_getter]` generate gains a paragraph after the consumer trait's
copied doc:
Provider trait for [`CanGreet`]. A provider imports this trait's
component key, [`GreeterComponent`], beside it.
The hover on a provider trait in a `use` line shows only the trait, so
its doc is where a reader learns which key to import beside it. The link
names the key that `name:` sets, so it resolves for a renamed key. The
line is added where the trait is emitted, not to the stored trait, whose
attributes the generated impls copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The provider trait's key doc line follows the consumer trait's doc after an empty line. A `#[doc(hidden)]` or other `#[doc(...)]` list carries no text, so it no longer counts as a doc to separate from, and a hidden trait's line opens its doc. A new snapshot pins that case, and the doc comment test's header now says the key line meets the user's doc on the provider trait. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
[claude-opus-5-5]
#[cgp_impl],#[cgp_provider], and#[cgp_new_provider]derive a provider's component key from the provider trait's path as the header writes it, soimpl crate::components::Greeternamescrate::components::GreeterComponentand the module needs no import of the key. The derived key carries the span of the trait's name, so a missing key is reported onGreeterin the header instead of on the whole attribute.Every generated provider trait also documents its key. The provider traits that
#[cgp_component],#[cgp_type], and#[cgp_getter]emit end their doc with "Provider trait for [CanGreet]. A provider imports this trait's component key, [GreeterComponent], beside it.", so hovering the trait in auseline shows which key to import.Motivation
A provider in another module than its component must import a key the source never writes, and before this PR nothing helped the reader find it. The macros derived an unqualified
{Trait}Componentspanned oncall_site, which failed the reader in three ways:E0425covered the whole attribute, and Rust Analyzer offered no fix on the header.Impact
A header that names its trait by path no longer needs the key's import. This holds for a local trait and a dependency's trait, and for paths starting with
crate::,super::,self::, or::. A header with the bare trait name behaves as before, apart from where the error points.One header form now fails: a path through a module that re-exports the trait without its key.
impl crate::facade::Greeternow looks forcrate::facade::GreeterComponentand reports "cannot find typeGreeterComponentin modulecrate::facade", so such a module should re-export the key with the trait. Released code is unaffected, since 0.7.0 could not derive a key from a path.Dependent repositories pass CI only after this PR merges. The doc line changes every provider trait in an expansion snapshot, so
cargo-cgp's re-blessed.expand.rsand.rust.stderrfixtures expect this branch, and the website's example code names a provider trait by path.cargo-cgpbuilds on the moved span. It reshapes a missing key at a header into[CGP-E041], with the import as a fix an editor applies on the trait's name, and a header whose derived name is not the component's into[CGP-E070].Areas needing attention
The key's span in
crates/macros/cgp-macro-core/src/types/cgp_provider/item.rsreplacescall_site, which the old comment kept so that Rust Analyzer's go-to-definition on the trait would not also offer the component struct. A current Rust Analyzer gives one target, the#[cgp_component]line where both items are defined, in the same crate and across crates; an older one may still show two.The doc line is added at emission, in
EvaluatedCgpComponent::to_documented_provider_trait, because the generated impls copy the stored provider trait's attributes. A#[doc(hidden)]trait gets the line without a separating blank line, since only a#[doc = "…"]string counts as a copied doc.